/*
 * Update an event
 */ 

// Create the calendar service object
var calendarService = new google.gdata.calendar.CalendarService('GoogleInc-jsguide-1.0');

// The default "private/full" feed is used to update existing event from the 
// primary calendar of the authenticated user
var feedUri = 'http://www.google.com/calendar/feeds/default/private/full';

// Full text query for this string
var searchText = 'JS-Client';

// Create a CalendarEventQuery, and specify that this query is 
// applied toward the "private/full" feed
var query = new google.gdata.calendar.CalendarEventQuery(feedUri);

// Set the query with the query text
query.setFullTextQuery(searchText);

// Flag to indicate whether a match is found
var eventFound = false;

// The first matched event entry will have its title updated to this string
var newTitle = 'JS-Client: update event';

// This callback method that will be called when getEventsFeed() returns feed data
var callback = function(result) {

  // Obtain the array of matched CalendarEventEntry
  var entries = result.feed.entry;

  // If there is matches for the full text query
  if (entries.length > 0) {

    // update the first matched event's title
    var event = entries[0];       
    event.setTitle(google.gdata.Text.create(newTitle));
    event.updateEntry(
        function(result) {
          PRINT('event updated!');
        }, 
        handleError
    );
  } else {
    // No match is found for the full text query
    PRINT('Cannot find event(s) with text: ' + searchText);
  }
}

// Error handler to be invoked when getEventsFeed() or updateEntry() 
// produces an error
var handleError = function(error) {
  PRINT(error);
}

// Submit the request using the calendar service object
calendarService.getEventsFeed(query, callback, handleError);
